#include "CSRs.h" 

#define NR_CSR 16
static CSR csrs[NR_CSR] = {};
static int nr_csrs=0;
void add_csr(const char *name,paddr_t addr, word_t date){
	assert(nr_csrs < NR_CSR);
	csrs[nr_csrs] = (CSR){.name = name,.address=addr,.date = date};	
	nr_csrs ++;
}

word_t read_csr(paddr_t addr){
	assert(addr < 4048);
	int inx = find_csr_by_addr(csrs,NR_CSR,addr);
	Assert(inx >=0,"the csr %x is not defined",addr);
	return csrs[inx].date;
}

void write_csr(paddr_t addr,word_t date){
	assert(addr < 4048);
	int inx = find_csr_by_addr(csrs,NR_CSR,addr);
	Assert(inx >=0,"the csr %x is not defined",addr);
	csrs[inx].date = date;
	//printf("address = %x\n",csrs[inx].address);
	//printf("data = %lx\n",csrs[inx].date);
}

void init_csrs()
{
	add_csr("mtvec",0x305,0);
	add_csr("mepc",0x341,0);
	add_csr("mstatus",0x300,0xa00001800);
	add_csr("mcause",0x342,0);
	add_csr("mie",0x304,0);
	add_csr("mip",0x344,0);
}

